package com.sisgesbyte.dao.seguridad;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sisgesbyte.dao.beanDao.RestriccionDetRequest;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.entity.seguridad.RestriccionesListaEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantenimientoRestriccionesDaoImpl extends AbstractFacade<RestriccionesListaEntity>  implements MantenimientoRestriccionesDao {

	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}
	
	public MantenimientoRestriccionesDaoImpl(){
		super(RestriccionesListaEntity.class);
	}
	
	@Override
	public List<RestriccionesListaEntity> listarRestricciones() {
		
		StringBuilder jpql = new StringBuilder();
			jpql.append("select r.id,r.empresa_id,e.razon_social,r.restriccion,r.active ");
			jpql.append("from seguridad.restricciones r ");
			jpql.append("inner join sistema.empresa e on r.empresa_id=e.id ");
		
		Query query = em.createNativeQuery(jpql.toString(),RestriccionesListaEntity.class);
	
		List<RestriccionesListaEntity> ls = query.getResultList();
		
		return ls;
	}

	@Override
	public GenericEntity guardarRestriccion(RestriccionDetRequest request) {
		
		StringBuilder jpql = new StringBuilder();
			jpql.append("select * from seguridad.sp_registrar_restricciones(:in_empresaid, :in_restriccion,:in_active)");
		
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_empresaid", request.getCodEmpresa());
			query.setParameter("in_restriccion", request.getRestriccion());
			query.setParameter("in_active", request.getActive());
			
		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity actualizarRestriccion(RestriccionDetRequest request) {
		StringBuilder jpql = new StringBuilder();
			jpql.append("select * from seguridad.sp_actualizar_restriccion(:in_id,:in_empresaid, :in_restriccion,:in_active)");
	
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_id", request.getId());
			query.setParameter("in_empresaid", request.getCodEmpresa());
			query.setParameter("in_restriccion", request.getRestriccion());
			query.setParameter("in_active", request.getActive());
		
		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity eliminarRestriccion(int codRestriccion) {
		
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from seguridad.sp_eliminar_restriccion (:in_id)");
	
	Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_id", codRestriccion);
		
		return (GenericEntity) query.getSingleResult();
	}

}
